using Nop.Core;
using Nop.Core.ApiDataModel;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Customers;
using Nop.Services.Customers;
using Nop.Services.Directory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Nop.Services.Orders;
using Spotshub.Api.Filter;
using Spotshub.Api.Models;

namespace Spotshub.Api.Controllers.V1
{
    [ExceptionHandler]
    public class RegionController : ApiController
    {
        private readonly ICustomerService _customerService;
        private readonly IShoppingCartService _shoppintCartService;
        private readonly IRegionService _regionService;
        private readonly ICountryService _countryService;
        private readonly IStateProvinceService _stateService;
        private readonly IWorkContext _workContext;

        public RegionController(IWorkContext workContext, IRegionService regionService,
            ICustomerService customerService, IShoppingCartService shoppintCartService,
            ICountryService countryService, IStateProvinceService stateService)
        {
            this._workContext = workContext;
            this._regionService = regionService;
            this._customerService = customerService;
            this._countryService = countryService;
            this._stateService = stateService;
            this._shoppintCartService = shoppintCartService;
        }

        // GET: api/region/get
        public IEnumerable<RegionModel> Get()
        {
            return _regionService.GetAllRegions().Select(r => new RegionModel(r));
        }

        // GET: api/region/getuserregion
        public RegionModel GetUserRegion()
        {
            if (_workContext.CurrentCustomer == null || _workContext.CurrentCustomer.IsGuest())
            {
                return new RegionModel() { Success = false, Message = "User not signin" };
            }
            return new RegionModel(_workContext.CurrentCustomer.Region) { Success = true };
        }

        [HttpPost]
        public RegionUpdateResult Update(int regionId)
        {
            if (_workContext.CurrentCustomer == null)
                return new RegionUpdateResult { Success = true };

            if (_workContext.CurrentCustomer.Region != null &&
                _workContext.CurrentCustomer.Region.Id == regionId)
                return new RegionUpdateResult { Success = true };

            var region = _regionService.GetRegionById(regionId);
            if (region == null)
            {
                return new RegionUpdateResult { Success = false, Error = "No such region" };
            }
            string sqlUpdateToRegion = "update Customer set Region_Id='" + regionId + "' where Id=" + _workContext.CurrentCustomer.Id;
            var bUpdate = _customerService.ExcuteSql(sqlUpdateToRegion, _workContext.CurrentCustomer);
            //_workContext.CurrentCustomer.Region = region;
            //_customerService.UpdateCustomer(_workContext.CurrentCustomer);
            if (bUpdate)
            {
                this._shoppintCartService.DeleteExpiredShoppingCartItems(DateTime.UtcNow);

                return new RegionUpdateResult { Success = true };
            }
            return new RegionUpdateResult { Success = false, Error = "update error!" };
        }


        [HttpGet]
        public ListResult<CountryModel> GetCountries()
        {
            var result = new ListResult<CountryModel>();
            var countries = _countryService.GetAllCountries(false);
            result.Items.AddRange(countries.Select(c => new CountryModel
            {
                Id = c.Id,
                Name = c.Name,
                TwoLetterISOCode = c.TwoLetterIsoCode,
                ThreeLetterISOCode = c.ThreeLetterIsoCode,
                DisplayOrder = c.DisplayOrder
            }));
            result.Success = true;
            return result;
        }

        [HttpGet]
        public ListResult<StateModel> GetProvinces(int countryId)
        {
            var result = new ListResult<StateModel>();
            var provinces = _stateService.GetStateProvincesByCountryId(countryId);
            if (provinces != null && provinces.Any())
            {
                //result.Items.AddRange(provinces.Select(p => new StateModel { Id = p.Id, Name = p.Name, Abbreviation = p.Abbreviation, DisplayOrder = p.DisplayOrder }));
                result.Items.AddRange(provinces.Where(p => p.Name != "Alberta").Select(p => new StateModel { Id = p.Id, Name = p.Name, Abbreviation = p.Abbreviation, DisplayOrder = p.DisplayOrder }));
                result.Success = true;
            }
            return result;
        }
    }
}
